package sort;

public class TestSort {

    public static void insertSort(int[] array){
        if (array == null || array.length < 2){
            return;
        }
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i;
            for (; j > 0; j--) {
                if(array[j-1] > tmp){
                    array[j] = array[j-1];
                }else{
                    break;
                }
            }
            array[j] = tmp;
        }
    }

    public static void shell(int[] array,int gap){
        if(array == null || array.length <2){
            return;
        }
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i;
            for (;j > gap-1 ; j -= gap) {
                if(tmp < array[j-gap]){
                    array[j] = array[j-gap];
                }else{
                    break;
                }
            }
            array[j] = tmp;
        }
    }

    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            shell(array,gap);
            gap /= 3;
        }
        shell(array,1);
    }

    public static void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void selectSort(int[] array){
        if (array == null || array.length < 2){
            return;
        }
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);//交换而不是覆盖
        }
    }

    //堆排序
    private static void shiftDown(int[] array,int root,int len){
        int child = 2*root+1;
        int parent = root;
        while(child < len){
            if(child+1 < len && array[child+1] > array[child]){
                child++;
            }
            if(array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else{
                break;//没有break他永远不会++，会陷入死循环
            }
        }
    }
    public static void heapSort(int[] array){
        //创建大根堆
        for (int i = (array.length-1-1)/2; i >= 0 ; i--) {
            shiftDown(array,i,array.length);
        }
        for (int i = array.length-1; i > 0; i--) {//已经获得一个大根堆了，直接交换,交换后在-1个的位置向下调整
            swap(array,0,i);
            shiftDown(array,0,i);
        }

//        int end = array.length-1;
//        while(end>=0){
//            swap(array,0,end);
//            shiftDown(array,0,end);
//            end--;
//        }
    }

    public static void bubbleSort(int[] array){
        for (int i = array.length-1; i > 0 ; i--) {
            boolean flg = true;
            for (int j = 0; j < i; j++) {
                if(array[j] > array[j+1]){
                    swap(array,j,j+1);
                    flg = false;
                }
            }
            //如果在一个循环里没有交换，则说明有序了
            if(flg){
                break;
            }
        }
    }
    //前后指针法
    private static int partition3(int[] array,int left,int right){
        int prev = left;
        int cur = left+1;
        while(cur <= right){
            if(array[cur] < array[left] && array[++prev] != array[left]){//前半段是去找小的数，后半段
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,left,prev);
        return prev;
    }
    //挖坑法的partition
    private static int partition2(int[] array,int left,int right){
        int tmp = array[left];
        while(left < right){
            while(left < right && array[right] <= tmp){
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] >= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    //Hoare法的partition
    private static int partition1(int[] array,int left,int right){
        int key = left;
        while(left < right){
            while(array[right] >= array[key] && left < right){//从后面开始找 从前面开始找有可能会碰到比key大的相遇情况
                right--;
            }
            while(array[left] <= array[key] && left < right){//得取等号
                left++;
            }
            swap(array,left,right);//交换后right肯定是大于key的
        }
        swap(array,key,right);
        return right;
    }
    public static void quickSort(int[] array,int left,int right){
        if(left >= right){
            return;
        }
        int pivot = partition1(array,left,right);//partition调整array
        quickSort(array,left,pivot-1);
        quickSort(array,pivot+1,right);
    }

    public static void main(String[] args) {
        int[] arr = {1,4,3,6,2};
        quickSort(arr,0,arr.length-1);
        for (int e:arr) {
            System.out.print(e);
        }
    }
}
